// pages/buycar/buycar.js
import http from '../../utils/http.js'
import BuyCart from '../../utils/buycar.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: app.globalData.imgDomain,
    goods:[],
    exgoods:[],
    loaded:false,
    imgHeight:0,
    showChoseCandle:false,
    candels:''
  },
  goAddOrder(e){
    if (!app.isBind()) {
      this.setData({
        isLogin: true
      });
      return;
    }

    let goodsData = this.data.goods;
    let hasCake = goodsData.find((item)=>{
      return item.catId!=22 && item.catId!=30;
    })
    if(!hasCake){
      wx.showToast({
        icon:'none',
        title: '蜡烛餐具不能单独购买哦'
      })
      return;
    }

    wx.navigateTo({
      url: '../order/addorder'
    })
  },

  goViewGood(e){
    let goodsId = e.currentTarget.dataset.goodsId
    let goodsName = e.currentTarget.dataset.goodsName
    let catId = e.currentTarget.dataset.catId
    if(catId==22 || catId==30){
      return;
    }
    wx.navigateTo({
      url: '../detail/detail?id=' + goodsId + '&goodsName=' + goodsName
    })
  },
  delGoodsNumber(e){
    let me = this;
    let userId = wx.getStorageSync("userId");
    http.request({
      url: 'cart/deleteGoodsNumber',
      data:{
        userId: userId,
        goodsAttrId:e.currentTarget.dataset.goodsAttrId
      },
      success: res => {
        me.loadCart();
      }
    });
  },

  addGoodsNumber(e){
    let me = this;
    let userId = wx.getStorageSync("userId");
    http.request({
      url: 'cart/addGoodsNumber',
      data: {
        userId: userId,
        goodsAttrId: e.currentTarget.dataset.goodsAttrId
      },
      success: res => {
        me.loadCart();
      }
    });
  },
  goHome(){
    wx.navigateBack({})
  },
  loadCart(){
    let me=this;
    let userId = wx.getStorageSync("userId");
    if (!userId){
      return;
    }
    http.request({
      url:'cart/getListByUserId/'+userId,
      method:'get',
      success:res=>{
        me.setData({
          goods:res.goods,
          totalAmount:res.totalPrice,
          loaded:true
        });
      }
    });
  },
  addCart(e) {
    let me = this;
    let userId = wx.getStorageSync("userId");
    if (e.currentTarget.dataset.goodsId == 80){
      wx.showModal({
        title: '温馨提示',
        content: '蛋糕内都附有餐具，确定额外购买餐具吗？',
        success:res=>{
          if(res.confirm){
            BuyCart.add(e.currentTarget.dataset.goodsAttrId, 1, userId, (res) => {
              wx.showToast({
                title: '已加入购物车'
              })
              me.loadCart();
            });
          }
        }
      })
    }else{
      BuyCart.add(e.currentTarget.dataset.goodsAttrId, 1, userId, (res) => {
        wx.showToast({
          title: '已加入购物车'
        })
        me.loadCart();
      });
    }
    
  },
  closeDialog(){
    this.setData({
      showChoseCandle:false
    });
  },
  showDialog(){
    this.loadCandles();
    this.setData({
      showChoseCandle: true
    });
  },
  /**
   * 加载餐具蜡烛
   */
  loadExgoods(){
    let me=this;
    http.request({
      url: 'cart/getExgoods/22',
      success: res => {
        me.setData({
          exgoods: res
        });
      }
    });
  },
  loadCandles() {
    let me = this;
    http.request({
      url: 'cart/getExgoods/30',
      success: res => {
        me.setData({
          candels: res
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let me = this;
    wx.setNavigationBarTitle({
      title: '购物车',
    })
    const column = 3;
    let systemInfo = wx.getSystemInfoSync();
    let width = systemInfo.screenWidth;
    let height = (width - (column + 1) * 10) / column;
    me.setData({
      imgHeight: height
    });
    me.loadExgoods();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      loaded:false
    });
    this.loadCart();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})